<?php declare(strict_types=1);
/**
 * This file is part of Swoft.
 *
 * @link     https://swoft.org
 * @document https://swoft.org/docs
 * @contact  group@swoft.org
 * @license  https://github.com/swoft-cloud/swoft/blob/master/LICENSE
 */

namespace App\Http\Controller\Test;

use Matrix\Exception;
use Swoft\Http\Message\Request;
use Swoft\Http\Server\Annotation\Mapping\Controller;
use Swoft\Http\Server\Annotation\Mapping\RequestMapping;
use Swoft\Http\Server\Annotation\Mapping\RequestMethod;
use Swoft\Validator\Annotation\Mapping\Validate;
use App\Exception\ApiException;

/**
 * https://www.wenjiangs.com/doc/vgepj9d2 【验证器文档】
 *
 * Class SwoftValidatorController
 *
 * @Controller(prefix="/Validator")
 */
class SwoftValidatorController
{
    /**
     * 验证测试验证程序中定义的所有字段
     *
     * @RequestMapping(route="validate_all")
     * @Validate(validator="TestValidator")
     *
     * @param Request $request
     *
     * @return array
     */
    public function validateAll(Request $request)
    {
        $method = $request->getMethod();
        if ($method == RequestMethod::GET) {
            return $request->getParsedQuery();
        }
        return $request->getParsedBody();
    }

    /**
     * 仅验证测试验证程序中的类型字段
     *
     * @RequestMapping(route="validate_type")
     * @Validate(validator="TestValidator", fields={"type"})
     *
     * @param Request $request
     *
     * @return array
     */
    public function validateType(Request $request)
    {
        $method = $request->getMethod();
        if ($method == RequestMethod::GET) {
            return $request->getParsedQuery();
        }
        return $request->getParsedBody();
    }

    /**
     * 仅验证测试验证程序中的密码字段
     *
     * @RequestMapping(route="validate_password")
     * @Validate(validator="TestValidator", fields={"password"})
     *
     * @param Request $request
     *
     * @return array
     */
    public function validatePassword(Request $request)
    {
        $method = $request->getMethod();
        if ($method == RequestMethod::GET) {
            return $request->getParsedQuery();
        }
        return $request->getParsedBody();
    }

    /**
     * 使用uservalidator定制验证器
     *
     * @RequestMapping(route="validate_customer")
     *
     * @Validate(validator="userValidator")
     *
     * @param Request $request
     *
     * @return array
     */
    public function validateCustomer(Request $request)
    {
        $method = $request->getMethod();
        if ($method == RequestMethod::GET) {
            return $request->getParsedQuery();
        }
        return $request->getParsedBody();
    }
}
